{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-03-05T13:23:18.709113Z",
     "start_time": "2024-03-05T13:23:18.700519Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "stock = \"hs300\""
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T13:23:18.739980Z",
     "start_time": "2024-03-05T13:23:18.730981Z"
    }
   },
   "id": "bdcdb64093f39df8",
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "pred = np.load(f\"{stock}_output/wealth4.npy\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T13:23:18.770295Z",
     "start_time": "2024-03-05T13:23:18.751592Z"
    }
   },
   "id": "cead1c0b55420ae1",
   "execution_count": 12
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "     daily_return\n0        1.000000\n1        1.008859\n2        1.027444\n3        1.027611\n4        1.025633\n..            ...\n236      0.862062\n237      0.871034\n238      0.865776\n239      0.864022\n240      0.883664\n\n[241 rows x 1 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>daily_return</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1.000000</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1.008859</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1.027444</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1.027611</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1.025633</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>236</th>\n      <td>0.862062</td>\n    </tr>\n    <tr>\n      <th>237</th>\n      <td>0.871034</td>\n    </tr>\n    <tr>\n      <th>238</th>\n      <td>0.865776</td>\n    </tr>\n    <tr>\n      <th>239</th>\n      <td>0.864022</td>\n    </tr>\n    <tr>\n      <th>240</th>\n      <td>0.883664</td>\n    </tr>\n  </tbody>\n</table>\n<p>241 rows × 1 columns</p>\n</div>"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pred = pd.DataFrame(pred.reshape(-1, 1), columns=['daily_return'])\n",
    "pred"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T13:23:18.785968Z",
     "start_time": "2024-03-05T13:23:18.772533Z"
    }
   },
   "id": "32bc234afc8ea279",
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "pred.to_csv(f\"{stock}_ARR.csv\", index=None)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T13:23:18.801857Z",
     "start_time": "2024-03-05T13:23:18.788087Z"
    }
   },
   "id": "7d82580bd2e953",
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "     daily_return\n1        0.008859\n2        0.018422\n3        0.000162\n4       -0.001925\n5        0.000182\n..            ...\n236      0.007665\n237      0.010408\n238     -0.006036\n239     -0.002026\n240      0.022734\n\n[240 rows x 1 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>daily_return</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>0.008859</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.018422</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0.000162</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>-0.001925</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>0.000182</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>236</th>\n      <td>0.007665</td>\n    </tr>\n    <tr>\n      <th>237</th>\n      <td>0.010408</td>\n    </tr>\n    <tr>\n      <th>238</th>\n      <td>-0.006036</td>\n    </tr>\n    <tr>\n      <th>239</th>\n      <td>-0.002026</td>\n    </tr>\n    <tr>\n      <th>240</th>\n      <td>0.022734</td>\n    </tr>\n  </tbody>\n</table>\n<p>240 rows × 1 columns</p>\n</div>"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pred['daily_return'] = pred['daily_return'] / pred['daily_return'].shift(1)\n",
    "pred['daily_return'] = pred['daily_return'] - 1\n",
    "pred.dropna(inplace=True)\n",
    "pred"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T13:23:18.832004Z",
     "start_time": "2024-03-05T13:23:18.803051Z"
    }
   },
   "id": "738a62bdd2c18e54",
   "execution_count": 15
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "portfolio_df_performance = pred"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T13:23:18.846652Z",
     "start_time": "2024-03-05T13:23:18.834006Z"
    }
   },
   "id": "7414f26b0c7add6f",
   "execution_count": 16
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\胡逸凡\\AppData\\Local\\Temp\\ipykernel_7844\\980728215.py:12: FutureWarning: 'm' is deprecated and will be removed in a future version, please use 'ME' instead.\n",
      "  monthly_statistics_df = alpha_df_performance[net_value_columns].resample('m').last()\n",
      "C:\\Users\\胡逸凡\\AppData\\Local\\Temp\\ipykernel_7844\\980728215.py:20: FutureWarning: 'y' is deprecated and will be removed in a future version, please use 'YE' instead.\n",
      "  yearly_statistics_df = alpha_df_performance[net_value_columns].resample('y').last()\n",
      "C:\\Users\\胡逸凡\\AppData\\Roaming\\Python\\Python310\\site-packages\\numpy\\core\\fromnumeric.py:3643: FutureWarning: The behavior of DataFrame.std with axis=None is deprecated, in a future version this will reduce over both axes and return a scalar. To retain the old behavior, pass axis=0 (or do not pass axis)\n",
      "  return std(axis=axis, dtype=dtype, out=out, ddof=ddof, **kwargs)\n",
      "C:\\Users\\胡逸凡\\AppData\\Roaming\\Python\\Python310\\site-packages\\numpy\\core\\fromnumeric.py:3643: FutureWarning: The behavior of DataFrame.std with axis=None is deprecated, in a future version this will reduce over both axes and return a scalar. To retain the old behavior, pass axis=0 (or do not pass axis)\n",
      "  return std(axis=axis, dtype=dtype, out=out, ddof=ddof, **kwargs)\n",
      "C:\\Users\\胡逸凡\\AppData\\Roaming\\Python\\Python310\\site-packages\\numpy\\core\\fromnumeric.py:3643: FutureWarning: The behavior of DataFrame.std with axis=None is deprecated, in a future version this will reduce over both axes and return a scalar. To retain the old behavior, pass axis=0 (or do not pass axis)\n",
      "  return std(axis=axis, dtype=dtype, out=out, ddof=ddof, **kwargs)\n"
     ]
    }
   ],
   "source": [
    "alpha_df_performance = pd.DataFrame()\n",
    "alpha_df_performance['portfolio_daily_return'] = portfolio_df_performance['daily_return']\n",
    "alpha_df_performance['portfolio_net_value'] = (alpha_df_performance['portfolio_daily_return'] + 1).cumprod()\n",
    "\n",
    "net_value_columns = ['portfolio_net_value']\n",
    "\n",
    "alpha_statistics_df = pd.DataFrame(index=alpha_df_performance[net_value_columns].columns,\n",
    "                                    columns=[\"年化收益\", \"年化波动率\", \"最大回撤率\", \"夏普率\", \"Calmar\", \"IR\"])\n",
    "\n",
    "# alpha_df_performance.set_index(\"dt\", inplace=True)\n",
    "alpha_df_performance.index = pd.to_datetime(alpha_df_performance.index)\n",
    "monthly_statistics_df = alpha_df_performance[net_value_columns].resample('m').last()\n",
    "monthly_statistics_df = pd.concat([alpha_df_performance[:1][\n",
    "                                        ['portfolio_net_value']],\n",
    "                                    monthly_statistics_df])\n",
    "monthly_statistics_df = monthly_statistics_df.pct_change()\n",
    "monthly_statistics_df = monthly_statistics_df.dropna()\n",
    "monthly_statistics_df.index = monthly_statistics_df.index.date\n",
    "## TODO 补充第一年的数据\n",
    "yearly_statistics_df = alpha_df_performance[net_value_columns].resample('y').last()\n",
    "yearly_statistics_df = pd.concat([alpha_df_performance[:1][\n",
    "                                        ['portfolio_net_value']],\n",
    "                                    yearly_statistics_df])\n",
    "yearly_statistics_df = yearly_statistics_df.pct_change()\n",
    "yearly_statistics_df = yearly_statistics_df.dropna()\n",
    "yearly_statistics_df.index = yearly_statistics_df.index.date\n",
    "\n",
    "alpha_statistics_df.loc[:, \"年化收益\"] = np.mean(\n",
    "    (alpha_df_performance[net_value_columns].tail(1)) ** (252 / len(alpha_df_performance)) - 1)\n",
    "alpha_statistics_df.loc[:, \"年化波动率\"] = np.std(\n",
    "    alpha_df_performance[net_value_columns] / alpha_df_performance[net_value_columns].shift(1) - 1) * np.sqrt(\n",
    "    252)\n",
    "alpha_statistics_df.loc[:, \"累积收益\"] = np.mean(alpha_df_performance[net_value_columns].tail(1) - 1)\n",
    "alpha_statistics_df.loc[:, \"累积波动率\"] = np.std(\n",
    "    alpha_df_performance[net_value_columns] / alpha_df_performance[net_value_columns].shift(1) - 1)\n",
    "alpha_statistics_df.loc[:, \"最大回撤率\"] = np.min(\n",
    "    (alpha_df_performance[net_value_columns] - alpha_df_performance[net_value_columns].cummax()) /\n",
    "    alpha_df_performance[net_value_columns].cummax())\n",
    "alpha_statistics_df.loc[:, \"夏普率\"] = alpha_statistics_df[\"年化收益\"] / alpha_statistics_df[\"年化波动率\"]\n",
    "alpha_statistics_df.loc[:, \"Calmar\"] = alpha_statistics_df[\"年化收益\"] / abs(alpha_statistics_df[\"最大回撤率\"])\n",
    "alpha_statistics_df.loc[:, \"IR\"] = np.mean(\n",
    "    alpha_df_performance[net_value_columns] / alpha_df_performance[net_value_columns].shift(1) - 1) * np.sqrt(\n",
    "    252) / np.std(alpha_df_performance[net_value_columns] / alpha_df_performance[net_value_columns].shift(1) - 1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T13:23:18.893067Z",
     "start_time": "2024-03-05T13:23:18.847821Z"
    }
   },
   "id": "d091b2a0f87d994a",
   "execution_count": 17
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "                         年化收益     年化波动率     最大回撤率       夏普率    Calmar  \\\nportfolio_net_value -0.121783  0.147073 -0.228532 -0.828046 -0.532894   \n\n                           IR      累积收益     累积波动率  \nportfolio_net_value -0.876184 -0.116336  0.009265  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>年化收益</th>\n      <th>年化波动率</th>\n      <th>最大回撤率</th>\n      <th>夏普率</th>\n      <th>Calmar</th>\n      <th>IR</th>\n      <th>累积收益</th>\n      <th>累积波动率</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>portfolio_net_value</th>\n      <td>-0.121783</td>\n      <td>0.147073</td>\n      <td>-0.228532</td>\n      <td>-0.828046</td>\n      <td>-0.532894</td>\n      <td>-0.876184</td>\n      <td>-0.116336</td>\n      <td>0.009265</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alpha_statistics_df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T13:23:18.908835Z",
     "start_time": "2024-03-05T13:23:18.894182Z"
    }
   },
   "id": "224d5e1186a559db",
   "execution_count": 18
  },
  {
   "cell_type": "markdown",
   "source": [
    "Deeptrader\n",
    "ASR = ARR / AVol;   CR = ARR / abs(MDD)\n",
    "        ARR    AVol    MDD    ASR    CR    IR\n",
    "hs300 -0.122  0.147  -0.229 -0.828 -0.533 -0.876\n",
    "\n",
    "zz500  0.055  0.168  -0.141  0.324  0.388  0.370\n",
    "\n",
    "sp500  0.295  0.180  -0.181  1.635  1.628  1.425\n",
    "   \n",
    "nas100 0.716  0.248  -0.138  2.890  5.169  2.306"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1e90408998bbd48b"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
